/**
 * Created by IntelliJ IDEA.
 * User: ???
 * Date: 2011-12-27
 */
package com.rocksea.app.rslib;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;

class JyQs extends JyCurve
{
	public JyQs(JyDoc doc)
	{
		super(doc);

		m_strScaleX = "Q (kN)";
		m_strScaleY = "s (mm)";
		m_strTitle = "Q-s曲线";
	}

	@Override
	protected void drawCurve(Canvas canvas)
	{
		Paint curPaint = new Paint();
		int nGradeCount = m_jyDoc.getJyDataInfo().nGradeCount;
		int nLoadGradeCount = m_jyDoc.getJyDataInfo().nLoadGradeCount;

		// ????
		PointF ptStart = getZeroPoint();
		for (int i = 0; i < nGradeCount; i++)
		{
			PointF ptEnd = getGradePoint(i);

			if (i < nLoadGradeCount)
			{
				curPaint.set(m_penLoad);
				curPaint.setColor(m_style.loadCurveStyle.lineColor);
			} else
			{
				curPaint.set(m_penUnload);
				curPaint.setColor(m_style.unloadCurveStyle.lineColor);
			}
			curPaint.setStrokeWidth(4.5f);// ???????
			canvas.drawLine(ptStart.x, ptStart.y, ptEnd.x, ptEnd.y, curPaint);

			ptStart.x = ptEnd.x;
			ptStart.y = ptEnd.y;
		}

		// ????
		curPaint.set(m_penDot);
		curPaint.setColor(m_style.dotStyle.dotColor);
		RectF ellipseRect = new RectF();
		for (int i = 0; i < nGradeCount; i++)
		{
			PointF pt = getGradePoint(i);

			ellipseRect.left = pt.x - m_style.dotStyle.dotDiameter / 2f;
			ellipseRect.top = pt.y - m_style.dotStyle.dotDiameter / 2f;
			ellipseRect.bottom = ellipseRect.top + m_style.dotStyle.dotDiameter;
			ellipseRect.right = ellipseRect.left + m_style.dotStyle.dotDiameter;

			if (m_style.dotStyle.isFilled)
			{
				curPaint.setStyle(Paint.Style.FILL);
				canvas.drawOval(ellipseRect, curPaint);
			} else
			{
				curPaint.setStyle(Paint.Style.STROKE);
				canvas.drawOval(ellipseRect, curPaint);
			}
		}
	}

	@Override
	protected void drawXCoordinate(Canvas canvas)
	{
		super.drawXCoordinate(canvas);

		Paint curPaint = new Paint();
		curPaint.setColor(m_style.scaleFont.fontColor);

		// ??????????????
		String strText = String.format("%.0f", m_fXMin);

		curPaint.setTypeface(m_fontScale);
		// curPaint.setStrokeWidth(m_style.scaleFont.fontHeight);
		curPaint.setTextSize(m_style.scaleFont.fontHeight);

		Rect rectText = new Rect();
		curPaint.getTextBounds(strText, 0, strText.length(), rectText);

		PointF pointText = new PointF();
		pointText.x = m_rcCurve.left - rectText.width() / 2f;
		pointText.y = m_isUp ? (m_rcCurve.bottom + m_style.longScale
				/ 2f + rectText.height())
				: (m_rcCurve.top - m_style.longScale * 1.5f);
		canvas.drawText(strText, pointText.x, pointText.y, curPaint);

		// ????????????
		strText = String.format("%.0f", m_fXMax);
		curPaint.getTextBounds(strText, 0, strText.length(), rectText);

		float fWidth = m_rcCurve.width() * 9f / 10f;
		pointText.x = m_rcCurve.left + fWidth - rectText.width() / 2f;
		canvas.drawText(strText, pointText.x, pointText.y, curPaint);

		if (m_jyDoc.getJyDataInfo().nGradeCount < 2)
			return;

		int nGradeCount = m_jyDoc.getJyDataInfo().nGradeCount;
		float[] fValue = new float[nGradeCount + 1];
		fValue[0] = m_fXMin;
		fValue[nGradeCount] = m_fXMax;

		// ??????????????????
		for (int i = 0; i < nGradeCount; i++)
		{
			float fQ = m_jyDoc.getJyData().get(i).fQGrade;

			boolean bIsMarked = false;
			for (float f : fValue)
			{
				if (Math.abs(fQ - f) < 0.01f)
					bIsMarked = true;
			}

			// ???????????
			if (!bIsMarked)
			{
				strText = String.format("%.0f", fQ);

				curPaint.setTypeface(m_fontScale);
				// curPaint.setStrokeWidth(m_style.scaleFont.fontHeight);
				curPaint.setTextSize(m_style.scaleFont.fontHeight);
				curPaint.getTextBounds(strText, 0, strText.length(), rectText);

				pointText.x = m_rcCurve.left + fWidth * fQ / m_fXMax
						- rectText.width() / 2f;
				canvas.drawText(strText, pointText.x, pointText.y, curPaint);

				fValue[i + 1] = fQ;
			}
		}

		// ??????????????????????????
		if (m_style.gridNum > 0)
			return;

		// ????????
		curPaint.set(m_penCoordinate);
		curPaint.setColor(m_style.coordinateStyle.lineColor);
		curPaint.setStrokeWidth(0f);// ?????????????

		float y1 = m_isUp ? (float) m_rcCurve.bottom : m_rcCurve.top;
		float y2 = m_isUp ? y1 + m_style.longScale : y1 - m_style.longScale;
		for (int i = 0; i < nGradeCount; i++)
		{
			float fQ = m_jyDoc.getJyData().get(i).fQGrade;

			if (fQ > 0)
			{
				float x = m_rcCurve.left + fWidth * fQ / m_fXMax;
				canvas.drawLine(x, y1, x, y2, curPaint);

			}
		}
	}

	protected PointF getGradePoint(int nGrade)
	{
		if (nGrade >= m_jyDoc.getJyData().size())
			return (new PointF(0f, 0f));

		float fWidth = m_rcCurve.width() * 0.9f;
		float fHeight = m_rcCurve.height() * 0.9f;

		float fXFullScale = m_fXMax - m_fXMin;
		float fYFullScale = m_fYMax - m_fYMin;

		float fXDelta = (m_jyDoc.getJyData().get(nGrade).fQGrade - m_fXMin)
				/ fXFullScale;
		float fYDelta = (m_jyDoc.getJyData().get(nGrade).fSEnd - m_fYMin)
				/ fYFullScale;

		PointF pt = new PointF();
		pt.x = m_rcCurve.left + fWidth * fXDelta;
		pt.y = m_isUp ? m_rcCurve.bottom - fHeight * fYDelta
				: m_rcCurve.top + fHeight * fYDelta;

		return pt;
	}
}
